package com.yanqu.road.dao.impl.activity.tonbing.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.tonbing.config.TonBingCityConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class TonBingCityConfigDaoImpl extends TempDao {

    private static String getTonBingCityConfigSql = "select * from `t_s_activity_peaktongbing_city` where `activityId`=?";

    public Map<Integer,Map<Integer, TonBingCityConfig>> getTonBingCityConfigMap(int activityId) {
        Map<Integer,Map<Integer, TonBingCityConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getTonBingCityConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TonBingCityConfig data = new TonBingCityConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setMapType(rs.getInt("mapType"));
                data.setType(rs.getInt("type"));
                data.setInitialCamp(rs.getInt("initialCamp"));
                data.setShowType(rs.getInt("showType"));

                String specialAddition = rs.getString("specialAddition");
                data.setSpecialAddition(specialAddition);
                Map<Integer, Integer> specAddiMap = new ConcurrentHashMap<>();
                if (!Objects.equals("0", specialAddition)) {
                    List<Integer> list = StringUtils.stringToIntegerList(specialAddition, ";");
                    specAddiMap.put(list.get(0), list.get(1));
                }
                data.setSpecialAdditionMap(specAddiMap);
                data.setWarType(rs.getInt("warType"));
                if(!map.containsKey(data.getWarType())){
                    map.put(data.getWarType(),new ConcurrentHashMap<>());
                }
                map.get(data.getWarType()).put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
